package com.zelin.manager.controller;

import com.zyg.common.utils.FastDFSClient;
import com.zyg.common.utils.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by WF on 2021/10/27 16:48
 */
@RestController
@RequestMapping("/manager")
public class UploadController {


    @PostMapping("/upload")
    public R upload(MultipartFile file) throws Exception {
        //1. 开始文件上传
        FastDFSClient client = new FastDFSClient("classpath:fast.conf");
        //2. 得到文件名
        String fileName = file.getOriginalFilename();
        //3. 得到文件后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
        //4. 得到上传的文件数据
        byte[] bytes = file.getBytes();
        //5. 开始文件上传
        String s = client.uploadFile(bytes, suffixName);
        //6. 组织url地址
        String url = "http://192.168.56.16:8080/" + s;
        //7. 返回
        return R.ok().put("url",url);
    }
}
